package view.panels;

import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;
import util.Consola;
import view.thread.CountFileThread;

/**
 *
 * @author
 * Guerino
 */
public class IndexJPanel extends javax.swing.JPanel {
    private File pathRoot;
    private DefaultTableModel tblModel;
    private Consola consola;
    private int contArchivos;
    /**
     * Creates
     * new
     * form
     * IndexJPanel
     */
    public IndexJPanel() {
        initComponents();
        this.jButtonStart.setEnabled(false);
        this.jProgressBarTotal.setVisible(false);
        this.pathRoot = null;
        this.contArchivos = 0;

        this.tblModel = new DefaultTableModel(null,getColumnas())
        {   @Override
            public boolean isCellEditable (int fila, int columna) {
                if (columna >= 3)
                    return true;
                return false;
            }
        };
        
        //Le seteamos el modelo
        this.jTableFolders.setModel(tblModel);
        
        //Para la salida de informacion
        this.consola = new Consola();
        this.consola.setTextArea(jTextAreaConsola);        
    }

    public JButton getButtonSeleccion() {
        return jButtonSeleccion;
    }

    public JButton getButtonStart() {
        return jButtonStart;
    }
  
    //Encabezados de la tabla
    private String[] getColumnas() {
        String columna[]=new String[]{"Ubicacion","Total archivos","Estado"};
        return columna;
    }   
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonSeleccion = new javax.swing.JButton();
        jButtonStart = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableFolders = new javax.swing.JTable();
        jPanelEstado = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaConsola = new javax.swing.JTextArea();
        jProgressBarTotal = new javax.swing.JProgressBar();

        jButtonSeleccion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/resource/archive-insert-directory.png"))); // NOI18N
        jButtonSeleccion.setText("Agregar directorio...");
        jButtonSeleccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSeleccionActionPerformed(evt);
            }
        });

        jButtonStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/resource/arrow-right.png"))); // NOI18N
        jButtonStart.setText("Iniciar");
        jButtonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartActionPerformed(evt);
            }
        });

        jTableFolders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Ubicacion", "Total archivos", "Total directorios"
            }
        ));
        jScrollPane3.setViewportView(jTableFolders);

        jPanelEstado.setBorder(javax.swing.BorderFactory.createTitledBorder("Consola"));

        jTextAreaConsola.setEditable(false);
        jTextAreaConsola.setBackground(new java.awt.Color(0, 0, 0));
        jTextAreaConsola.setColumns(20);
        jTextAreaConsola.setForeground(new java.awt.Color(51, 204, 0));
        jTextAreaConsola.setRows(5);
        jTextAreaConsola.setDoubleBuffered(true);
        jScrollPane1.setViewportView(jTextAreaConsola);

        javax.swing.GroupLayout jPanelEstadoLayout = new javax.swing.GroupLayout(jPanelEstado);
        jPanelEstado.setLayout(jPanelEstadoLayout);
        jPanelEstadoLayout.setHorizontalGroup(
            jPanelEstadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
        );
        jPanelEstadoLayout.setVerticalGroup(
            jPanelEstadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSeleccion)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonStart, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(jProgressBarTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(14, 14, 14))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanelEstado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE))
                        .addGap(10, 10, 10))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSeleccion)
                    .addComponent(jButtonStart)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jProgressBarTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
        //LLamar al hilo de indexacion
        //IndexThread hilo = new IndexThread(this.pathRoot, this.jProgressBarPercentage, this.jTextAreaInfo);
        this.jProgressBarTotal.setVisible(true); 
       // hilo.start();
        //GestorDirectorio.indizar(DirectorioInicial);
        
       //Mostramos el estado de la indexacion
      // this.jTableFolders.setSize(this.jTableFolders.getWidth(),this.jTableFolders.getHeight()-40 );
      // this.jPanelEstado.setVisible(true);
    }//GEN-LAST:event_jButtonStartActionPerformed

    private void jButtonSeleccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSeleccionActionPerformed
        //Seleccionamos solo directorios
        JFileChooser jFChooser = new JFileChooser();
        jFChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);        
        int n = jFChooser.showOpenDialog(null);
  
        this.pathRoot = jFChooser.getSelectedFile();
       
        //Creamos y lanzamos el hilo de conteo de archivos
        CountFileThread hilo = new CountFileThread(this, jTextAreaConsola, tblModel, pathRoot);        
        hilo.start();
    }//GEN-LAST:event_jButtonSeleccionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSeleccion;
    private javax.swing.JButton jButtonStart;
    private javax.swing.JPanel jPanelEstado;
    private javax.swing.JProgressBar jProgressBarTotal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTableFolders;
    private javax.swing.JTextArea jTextAreaConsola;
    // End of variables declaration//GEN-END:variables
}
